<?php
/*
 * Created on Feb 3, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
	$title = 'Tài khoản';
 	include('../includes/session.php');
	if($session->logged_in){
		if($session->userlevel == 1){
			header("Location: ../index.php");
		}
	}else{
		header("Location: login.php");
	}
	include('../includes/encrypturl.php');
	include('../models/userinfo_model.php');
	$session->setHeader(6);	
 	include('../includes/header.inc');
	
	if (isset($_GET['SelectedUser'])){
		$SelectedUser =strtoupper($_GET['SelectedUser']);
	} elseif(isset($_POST['SelectedUser'])){
		$SelectedUser =strtoupper($_POST['SelectedUser']);
	}
	
	echo '<div id="wrapper">';
	echo '<div id="content">';
	echo '<div id="box">';

	if (isset($_GET['delete']) && $SelectedUser) {
		if($userModel->deleteUser($SelectedUser) == 0){
			echo prnMsg(_('Tài khoản < '.$SelectedUser.' > đã xóa.'),'success');
		}else{
			echo prnMsg(_('Tài khoản < '.$SelectedUser.' > chưa xóa.'),'success');
		}				
		unset ($SelectedUser);
		unset($delete);
	}
	
	if (!isset($SelectedUser)) {
		
		$start=$_GET['start'];
		if(strlen($start) > 0 and !is_numeric($start)){
			echo "Data Error";
			exit;
		}		
		
		$eu = ($start - 0); 
		$limit = $number_record_perpage;                                 
		$this1 = $eu + $limit; 
		$back = $eu - $limit; 
		$next = $eu + $limit;
		
		$num = $userModel->getCountUser();	
		$result = $userModel->getUsers($eu, $limit);		
		
	    echo '<h3>Danh sách tài khoản</h3>';
	   	echo '<table width="100%"><thead>';
			echo '<tr class="borderBottom">';
	        echo '<th><a href="#">ID<img src="'.$PathPrefix.'img/icons/arrow_down_mini.gif" width="16" height="16" align="absmiddle" /></a></th>';
	        echo '<th><a href="#">Họ tên</a></th>';
	        echo '<th><a href="#">Email</a></th>';
	        echo '<th><a href="#">Địa chỉ</a></th>';
	        echo '<th><a href="#">Điện thoại</a></th>';
	        echo '<th><a href="#">Ngày đăng ký</a></th>';
			echo '<th><a href="#">Ngày cập nhật</a></th>';	        
	        echo '<th><a href="#">Hành động</a></th>';
	        echo '</tr></thead>';
			
			while ($myrow = $userModel->getArrayUsers($result)) {
				$isActive = 'inactive';
				if($myrow[8] == 1) $isActive = 'active';
				printf('<tbody><tr class="borderBottom">
						<td class="a-center">%s</td>
						<td><a href="#">%s</a></td>
	                    <td>%s</td>
	                    <td>%s</td>
	                    <td>%s</td>
	                    <td>%s</td>
	                    <td>%s</td>						
	                    <td><a href="%sSelectedUser=%s"><img src="'.$PathPrefix.'img/icons/user_edit.png" title="Edit user" width="16" height="16" /></a>
							<a href="%sSelectedUser=%s&delete=1"><img src="'.$PathPrefix.'img/icons/user_delete.png" title="Delete user" width="16" height="16" /></a></td>
	                    </tr></tbody>',
						$myrow[0],
						$myrow[3],
						$myrow[6],
						$myrow[5],
						$myrow[4],
						ConvertSQLDate($myrow[11]),
						ConvertSQLDate($myrow[12]),	
						$_SERVER['PHP_SELF'] . '?' . SID . '&',
						$myrow[0],
						$_SERVER['PHP_SELF'] . '?' . SID . '&',
						$myrow[0]);				
			}
			
		echo '</table>';
		if($num > $limit ){
			echo '<div id="pager">Trang ';
			if($back >=0) {  
				echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$back.'"><img src="'.$PathPrefix.'img/icons/arrow_left.gif" width="16" height="16" /></a>';
			};
			$i=0;
			$l=1;
			for($i=0;$i < $num;$i=$i+$limit){
				if($i <> $eu){
					echo " <a href='".$_SERVER['PHP_SELF']."?start=$i'><font face='Verdana' size='2'>$l</font></a> ";
				}
				else { 
					echo "<font face='Verdana' size='4' color=red>$l</font>";				
				}
					$l=$l+1;
			}
			if($this1 < $num) {
				echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.$next.'"><img src="'.$PathPrefix.'img/icons/arrow_right.gif" width="16" height="16" /></a>';
			};
            
            echo ' Tổng cộng <strong>'.$num.'</strong> tài khoản
                    </div>';
		}else{
			echo '<div id="pager">Tổng cộng <strong>'.$num.'</strong> tài khoản</div>';
		}
	}
	if (isset($SelectedUser)) {
		echo "<div><a href='" . $_SERVER['PHP_SELF'] . '?' . SID . "'><font face='Verdana' size='2' color=red>" . _('Quay về danh sách tài khoản') . "</font></a></div>";
	}
	echo '</div>';
	echo '<br />';
	echo '<div id="box">';
	
	if(isset($_SESSION['success'])){
	   /* Registration was successful */
	   if($_SESSION['success']){
	   		echo prnMsg(_('Tài khoản < '.$_SESSION['successname'].' > đã tạo thành công.'),'success');
	   }
	   /* Registration failed */
	   else{
	      echo prnMsg(_('Tài khoản < '.$_SESSION['successname'].' > không thể tạo'),'success');
	   }
	   unset($_SESSION['success']);
	   unset($_SESSION['successname']);
	}
	
	if(isset($_SESSION['edit'])){
   		unset($_SESSION['edit']);
   		echo prnMsg(_('Tài khoản < '.$_SESSION['editname'].' > cập nhật thành công'),'success');
		unset($_SESSION['editname']);
	}

	if($form->num_errors > 0){
	   //echo $form->num_errors." error(s) found";
	   echo $form->error("SqlErrorMessage");
	   echo $form->error("fullName");
	   echo $form->error("userName");
	   echo $form->error("password");
	   echo $form->error("email");
	   echo $form->error("place");
	   echo $form->error("phone");
	}

	echo "<form id='form' method='post' action='../controllers/userinfo_controller.php" . "?" . SID . "'>";
		
		if (isset($SelectedUser)) {			
			$myrow = $userModel->getUserById($SelectedUser);

			$_POST['userName'] = $myrow['userName'];
			$_POST['password'] = $myrow['password'];
			$_POST['fullName'] = $myrow['fullName'];
			$_POST['phone'] = $myrow['phone'];
			$_POST['place'] = $myrow['place'];
			$_POST['email'] = $myrow['email'];		
						
			echo "<input type=hidden name='SelectedUser' VALUE='" . $SelectedUser . "'>";
			
			echo '<h3 id="adduser">Cập nhật tài khoản</h3><br/>';			
		}else{
			// khong chon select user
			echo '<h3 id="adduser">Thêm tài khoản</h3><br/>';
		}		
		if (!isset($_POST['createDate'])){
	  		$_POST['createDate']= $today;
		}
		
		echo '<fieldset id="personal">';
        echo '<legend>THÔNG TIN CHUNG</legend>';
		echo '<label for="fullName">Họ tên<span class="require">(*)</span>: </label><input name="fullName" id="fullName" type="text" tabindex="1" size="70" value="'.(isset($SelectedUser)?$_POST['fullName']:$form->value("fullName")).'"/><br />';
		
		if (isset($SelectedUser)) {
			echo '<input type="hidden" name="userName" id="userName" type="text" tabindex="2" size="70" value="'.$_POST['userName'].'"/>';
			echo '<input type="hidden" name="useredit" value="1" />';			
		}else{
			echo '<label for="userName">Tài khoản<span class="require">(*)</span>: </label><input name="userName" id="userName" type="text" tabindex="2" size="70" value="'.(isset($SelectedUser)?$_POST['userName']:$form->value("userName")).'"/><br />';
			echo '<input type="hidden" name="userregister" value="1" />';
			
		}
		echo '<label for="password">Mật khẩu<span class="require">(*)</span>: </label><input name="password" id="password" type="password" tabindex="3" size="30" value=""/><br />';
		echo '<label for="password2">Nhập lại: </label><input name="password2" id="password2" type="password" tabindex="4" size="30" /><br />';
		echo '<input type="hidden" name="statusFlag" value="1" />';
		echo '<label for="email">Email<span class="require">(*)</span>: </label><input name="email" id="email" type="text" tabindex="5" size="30" value="'.(isset($SelectedUser)?$_POST['email']:$form->value("email")).'"/><br />';
		echo '</fieldset>';
		echo '<fieldset id="personal">';
        echo '<legend>THÔNG TIN LIÊN LẠC</legend>';
		echo '<label for="place">Địa chỉ<span class="require">(*)</span>: </label><input name="place" id="place" type="text" tabindex="6" size="70" value="'.(isset($SelectedUser)?$_POST['place']:$form->value("place")).'"/><br />';
		echo '<label for="phone">Điện thoại<span class="require">(*)</span>: </label><input name="phone" id="phone" type="text" tabindex="7" size="30" value="'.(isset($SelectedUser)?$_POST['phone']:$form->value("phone")).'"/><br />';
		if (!isset($SelectedUser)) {
			$createDate = (!isset($SelectedUser)?$_POST['createDate']:$form->value("createDate"));
			echo '<label for="createDate">Ngày đăng ký: </label><input name="createDate" id="createDate" readonly="readonly" type="text" tabindex="7" size="15" value="'.$createDate.'"/>';
			echo '<a href="javascript:NewCal(\'createDate\', \'ddmmyyyy\', false, 24, \'..\');"><img border="0" src="../img/cal.jpg" alt="" /></a><br />';
		}
		/*echo '<label for="statusFlag">Trạng thái: </label><select name="statusFlag">';
			$status = (isset($SelectedUser)?$_POST['statusFlag']:$form->value("statusFlag"));
			if($status == 1)
				echo '<option selected="selected" value="1">Hoạt động</option>';
			else echo '<option value="1">Hoạt động</option>';
            if($status == 0)
				echo '<option selected="selected" value="0">Không hoạt động</option>';
			else echo '<option value="0">Không hoạt động</option>';
           	echo '</select><br />';*/
		echo '</fieldset>';
		echo '<div align="center">
        		<input name="submit" type="submit" value="Đồng ý" /> 
                <input name="reset" type="reset"  value="Thiết lập lại"/>
             </div></form>';
	
	echo '</div>';
	echo '</div>';	
	include('../includes/footer.inc');
?>